package com.cskaoyan.service.wx_address;

import com.cskaoyan.bean.mall.region.Region;
import com.cskaoyan.bean.mall.region.RegionExample;
import com.cskaoyan.bean.user.Address;
import com.cskaoyan.bean.user.AddressExample;
import com.cskaoyan.bean.wx_address.AddressVO;
import com.cskaoyan.mapper.mall.RegionMapper;
import com.cskaoyan.mapper.user.AddressMapper;
import com.cskaoyan.utils.Constant;
import com.cskaoyan.utils.ReflectionSetJavaBeanUtils;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Repository
@Transactional
public class WxWxAddressServiceImpl implements WxAddressService {
    @Resource
    AddressMapper addressMapper;
    @Resource
    RegionMapper regionMapper;

    @Override
    public List<AddressVO> queryAddressByUserId(int userId) {
        List<AddressVO> addressVOS = addressMapper.selectAddress(null, userId);
        setAddressVOS(addressVOS);
        return addressVOS;
    }

    @Override
    public int createAddress(Address address) {
        //修改默认地址
        if (address.getIsDefault()) changeDefaultAddress();
        setAddress(address);
//        AddressExample addressExample = new AddressExample();
//        AddressExample.Criteria criteria = addressExample.createCriteria();
//        criteria.andUserIdEqualTo(address.getUserId());
//        criteria.andDeletedEqualTo(false);
//        List<Address> addresses = addressMapper.selectByExample(addressExample);
//        if (addresses.size() == 1) return -1;
        if (address.getId() != 0) {
            int i = addressMapper.updateByPrimaryKeySelective(address);
            if (i == 1) return Constant.SUCCESS;
            return Constant.FAIL;
        }
        address.setId(null);
        int i = addressMapper.insertSelective(address);
        return address.getId();
    }

    @Override
    public int deleteAddress(Integer id) {
        Address OldAddress = addressMapper.selectByPrimaryKey(id);
        //假如是默认地址，不能删除
//        if (OldAddress.getIsDefault()) return Constant.FAIL;
        Address address = new Address();
        address.setId(id);
        ReflectionSetJavaBeanUtils.setBeanDeletedValue(address);
        int i = addressMapper.updateByPrimaryKeySelective(address);
        if (i == 1) return Constant.SUCCESS;
        return Constant.FAIL;
    }

    @Override
    public AddressVO queryAddressDetail(Integer id) {
        List<AddressVO> addressVOS = addressMapper.selectAddress(id, null);
        setAddressVOS(addressVOS);
        if (addressVOS.size() == 0) return null;
        return addressVOS.get(0);
    }

    private void setAddressVOS(List<AddressVO> addressVOS) {
        for (AddressVO addressVO : addressVOS) {
            List<Region> regions = addressVO.getRegions();
            for (Region region : regions) {
                switch (region.getType()) {
                    case 3:
                        addressVO.setCounty(region.getName());
                        break;
                    case 2:
                        addressVO.setCity(region.getName());
                        break;
                    case 1:
                        addressVO.setProvince(region.getName());
                }
            }
        }
    }

    private void setAddress(Address address) {
        ReflectionSetJavaBeanUtils.setJavaBean(address);
        RegionExample regionExample = new RegionExample();
        RegionExample.Criteria criteria = regionExample.createCriteria();
        LinkedList<String> strings = new LinkedList<>();
        strings.add(address.getProvince());
        strings.add(address.getCity());
        strings.add(address.getCounty());
        criteria.andNameIn(strings);
        List<Region> regions = regionMapper.selectByExample(regionExample);
        for (Region region : regions) {
            switch (region.getType()) {
                case 3:
                    address.setCounty(region.getCode().toString());
                    break;
                case 2:
                    address.setCity(region.getCode().toString());
                    break;
                case 1:
                    address.setProvince(region.getCode().toString());
            }
        }
    }

    private void changeDefaultAddress() {
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        Address address = new Address();
        address.setIsDefault(false);
        addressMapper.updateByExampleSelective(address, addressExample);
    }
}
